import logging
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import exception_handler

logger = logging.getLogger('response_handle')

def custom_exception_handler(exc, context):
    response = exception_handler(exc, context)

    logger.error(context)

    if response:
        return Response({'message': '服务器错误', 'view': f'{context["view"]}', 'detail': f'{context["request"]}', 'error': f'{exc}'}, status=response.status_code)

    return Response({'message': '服务器错误', 'view': f'{context["view"]}', 'detail': f'{context["request"]}', 'error': f'{exc}'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)